using SG;
using System.Collections.Generic;
using UnityEngine;
using static UnityEditor.PlayerSettings;

public class GridRender : MonoBehaviour
{
    public GameObject m_Line;
    public Vector2 m_Center;
    public int m_Col = 10;
    public int m_Row = 10;
    public Vector2 m_GridSize = Vector2.one; //  

    List<LineRenderer> m_LineList = new List<LineRenderer>();

    List<Vector2> top_edge = new List<Vector2>();
    List<Vector2> bottom_edge = new List<Vector2>();
    List<Vector2> left_edge = new List<Vector2>();
    List<Vector2> right_edge = new List<Vector2>();

    ModifyBase m_LineModify = new ModifyBase();
    
    void Start()
    {
        float height = m_GridSize.y * m_Row;
        float width = m_GridSize.x * m_Col;
        for (int i = 0; i <= m_Col; ++i)
        {
            var pos = new Vector2();
            pos.x = m_Center.x + i * m_GridSize.x - width / 2;
            pos.y = m_Center.y - height / 2;
            bottom_edge.Add(pos);
            pos.y = m_Center.y + height / 2;
            top_edge.Add(pos);
        }
        for (int i = 0; i <= m_Row; ++i)
        {
            var pos = new Vector2();
            pos.x = m_Center.x - width / 2;
            pos.y = m_Center.y + m_GridSize.y * i - height / 2;
            left_edge.Add(pos);
            pos.x = m_Center.x + width / 2;
            right_edge.Add(pos);
        }
        m_LineModify.MakeModify();
    }
    List<Vector3> posList = new List<Vector3>();
    int m_nIndex = 0;
    void SetLinePosition(Vector3 start, Vector3 end)
    {
        if( m_LineList.Count >= m_nIndex )
        {
            var line = ObjectMgr.Instance().CreateObj(m_Line, transform);
            
            m_LineList.Add(line.GetComponent<LineRenderer>());
        }
        m_LineList[m_nIndex].positionCount = 2;
        m_LineList[m_nIndex].SetPosition(0, start);
        m_LineList[m_nIndex].SetPosition(1, end);
        ++m_nIndex;
    }
    void FreshLineData()
    {
        if (m_LineModify.IsModify)
        {
            m_nIndex = 0;

            for (int i = 0; i <= m_Col; ++i)
            {
                SetLinePosition(bottom_edge[i], top_edge[i]);
                //posList.Add( bottom_edge[i] ) ;
                //posList.Add( top_edge[i] );
                //if (i < m_Col)
                //{
                //    posList.Add(top_edge[i + 1]);
                //    posList.Add(bottom_edge[i + 1]);
                //}
            }
            for (int i = 0; i <= m_Row; ++i )
            {
                SetLinePosition(right_edge[i], left_edge[i]);
                //posList.Add(right_edge[i]);
                //posList.Add(left_edge[i]);
                //if (i < m_Row)
                //{
                //    posList.Add(left_edge[i + 1]);
                //    posList.Add(right_edge[i + 1]);
                //}
            }
            m_LineModify.ClearModify();
        }
        //if (m_Line)
        //{
        //    m_Line.positionCount = posList.Count;
        //    m_Line.SetPositions(posList.ToArray());
        //}
    }

    // Update is called once per frame
    void Update()
    {
        FreshLineData();
        if (Input.GetKeyDown(KeyCode.Space))
        {
            //if (m_Line)
            //{
            //    m_Line.enabled = !m_Line.enabled;
            //}
        }
    }
}
